/*
 * 用户管理页面
 */
Ext.define('MyApp.view.accountManager', {
    extend: 'Ext.panel.Panel',
    id: 'accountManager',
    closable: true,
    title: '管理用户信息',
	toolbar: null,
	getToolbar: function(){
		var me = this;
		if(Ext.isEmpty(me.toolbar)){
			me.toolbar = Ext.widget('toolbar',{
				dock: 'top',
				items: [{
					xtype: 'button',
					itemId: 'editButton',
					text: '编辑',
					listeners: {
						click: {
							fn: me.modifyUser,
							scope: me
						}
					}
				},'-',{
					xtype: 'button',
					itemId: 'enableButton',
					text: '启用',
					listeners: {
						click: {
							fn: me.enableUser,
							scope: me
						}
					}
				},'-',{
					xtype: 'button',
					itemId: 'disableButton',
					text: '禁用',
					listeners: {
						click: {
							fn: me.disableUser,
							scope: me
						}
					}
				}]
			});
		}
		return me.toolbar;
	},
	userWindow: null,
	getUserWindow: function(){
		if(this.userWindow == null){
        	this.userWindow = new MyApp.view.accountWindow();
        }
		return this.userWindow;
	},
    // 编辑用户
    modifyUser: function(button, e, eOpts) {
		var grid = this.getUserGrid(),
			selModel = grid.getSelectionModel(),
			selRecords = selModel.getSelection(),
			window = this.getUserWindow();
    	if(selRecords == null||selRecords.length<=0){
    		Ext.ux.Toast.msg("温馨提示", "请先选择要编辑的用户");
    		return;
    	}
    	window.bindData(selRecords[0],true);
		window.show();
    },
    // 禁用用户
    disableUser: function(button, e, eOpts) {
    	var grid = this.getUserGrid(),
			selModel = grid.getSelectionModel(),
			selRecords = selModel.getSelection();
    	if(selRecords == null||selRecords.length<=0){
    		Ext.ux.Toast.msg("温馨提示", "请先选择要禁用的用户");
    		return;
    	}
		var record = selRecords[0];
		if(record.get('isDeleted')){
			Ext.ux.Toast.msg("温馨提示", "该用户已经禁用！");
			return;
		}
    	Ext.MessageBox.confirm('温馨提示', '确定禁用该用户吗？', function(btn){
			if(btn == 'yes'){
				Ext.Ajax.request({
					url : './rs/account/disableUser',
					params: {'id': record.get('id')},
					success : function(response, opts) {
						Ext.ux.Toast.msg("温馨提示", "禁用成功！");
						grid.getStore().load();
						grid.getSelectionModel().deselectAll();
					},
					exception : function(response, opts) {
						var json = Ext.decode(response.responseText);
						Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
					}
				});
    		}else{
    			return;
    		}
		});
    },
	// 启用用户
    enableUser: function(button, e, eOpts) {
    	var grid = this.getUserGrid(),
			selModel = grid.getSelectionModel(),
			selRecords = selModel.getSelection();
    	if(selRecords == null||selRecords.length<=0){
    		Ext.ux.Toast.msg("温馨提示", "请先选择要启用的用户");
    		return;
    	}
		var record = selRecords[0];
		if(!record.get('isDeleted')){
			Ext.ux.Toast.msg("温馨提示", "该用户已经启用！");
			return;
		}
    	Ext.MessageBox.confirm('温馨提示', '确定启用该用户吗？', function(btn){
			if(btn == 'yes'){
				Ext.Ajax.request({
					url : './rs/account/enableUser',
					params: {'id': record.get('id')},
					success : function(response, opts) {
						Ext.ux.Toast.msg("温馨提示", "启用成功！");
						grid.getStore().load();
						grid.getSelectionModel().deselectAll();
					},
					exception : function(response, opts) {
						var json = Ext.decode(response.responseText);
						Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
					}
				});
    		}else{
    			return;
    		}
		});
    },
	queryForm: null,
	getQueryForm: function(){
		var me = this;
		if(me.queryForm==null){
			me.queryForm = Ext.widget('form',{
                layout: 'column',
				defaults: {
					margin: '10 10 10 10',
					columnWidth: .2,
					labelWidth: 80,
					xtype: 'textfield'
				},
                items: [{
					name: 'realName',
					fieldLabel: '姓名'
				}],
                buttons: [{
					text:'查询',
					handler: function() {
						me.getPagingToolbar().moveFirst();
					}
				},'-',{
					text:'重置',
					handler: function() {
						me.getQueryForm().getForm().reset();
					}
				},'->']
            });
		}
		return this.queryForm;
	},
	pagingToolbar: null,
	getPagingToolbar: function(){
		var me = this;
		if(me.pagingToolbar==null){
			me.pagingToolbar = Ext.widget('pagingtoolbar',{
				store: me.getStore(),
				dock: 'bottom',
				displayInfo: true
			});
		}
		return me.pagingToolbar;
	},
	store: null,
	getStore: function(){
		var me = this;
		if(me.store==null){
			me.store = Ext.create('MyApp.store.AccountStore',{
				autoLoad: true,
				listeners: {
					beforeload : function(store, operation, eOpts) {
						var queryForm = me.getQueryForm();
						if (queryForm != null) {
							var values = queryForm.getValues();
							Ext.apply(operation, {
								params: {
									'realName': Ext.String.trim(values.realName)
								}
							});	
						}
					}
				}
			});
		}
		return me.store;
	},
	userGrid: null,
	getUserGrid: function(){
		var me = this;
		if(Ext.isEmpty(me.userGrid)){
			me.userGrid = Ext.widget('gridpanel',{
                header: false,
                columnLines: true,
				store: me.getStore(),
				columns: [{
					xtype: 'rownumberer'
				},{
					dataIndex: 'nickName',
					flex: 0.5,
					align: 'center',
					text: '姓名'
				},{
					dataIndex: 'userId',
					flex: 1,
					align: 'center',
					text: '用户id'
				},{
					dataIndex: 'appKey',
					flex: 1,
					align: 'center',
					text: 'appKey'
				},{
					dataIndex: 'createTime',
					flex: 1,
					align: 'center',
					text: '创建时间'
				},{
					dataIndex: 'lastUpdateTime',
					text: '最后更新时间',
					align: 'center',
					flex: 2
				},{
					dataIndex: 'isDeleted',
					text: '是否启用',
					flex: 1,
					align: 'center',
					renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
		            	return CommonFunction.rendererEnable(value);
		            }
				}],
				dockedItems: [me.getToolbar(),me.getPagingToolbar()]
			});
		}
		return me.userGrid;
	},
    initComponent: function() {
        var me = this;
        Ext.applyIf(me, {
            items: [me.getQueryForm(),me.getUserGrid()]
        });
        me.callParent(arguments);
    }
});

Ext.define('MyApp.view.accountWindow', {
    extend: 'Ext.window.Window',
    width: 700,
    title: '新增/修改用户',
	closeAction: 'hide',
	modal: true,
	form: null,
	getForm: function(){
		var me = this;
		if(me.form==null){
			me.form = Ext.widget('form',{
                    layout: 'column',
					defaults: {
						margin: '5 5 5 5',
						labelWidth: 85,
						xtype: 'textfield'
					},
                    items: [{
						name: 'nickName',
						allowBlank: false,
						columnWidth: .25,
						fieldLabel: '真实姓名'
					},{
						name: 'userId',
						allowBlank: false,
						columnWidth: .25,
						fieldLabel: '用户id'
					},{
						name: 'appKey',
						allowBlank: false,
						columnWidth: .5,
						fieldLabel: 'appKey'
					},{
					    xtype: 'filefield',
					    name: 'avatarUrl',
					    columnWidth: 1,
					    vtype: 'image',
					    fieldLabel: '头像图片'
					}],
                    buttons: [{
						text:'保存',
						formBind: true,
						disabled: true,
						handler: function() {
							var tab = Ext.getCmp('accountManager'),
								userGrid = tab.getUserGrid(),
								userGridStore = userGrid.getStore(),
								form = me.getForm(),
								record = form.getRecord(),
								url = './rs/account/addUser',params,
								message = '新增';
							if(!form.isValid()){
								return;
							}
							form.updateRecord(record);
							var params = {
									'realName': record.get('realName'),
							};
							if(me.isUpdate){
								url = './rs/account/updateUser';
								message = '修改';
								Ext.Object.merge(params,{
									'id': record.get('id')
								});
							}
							
							form.submit({
								url : url,
								waitMsg: '正在保存。。。',
								params: params,
								success : function(from, action, json) {
									var accountManager = Ext.getCmp('accountManager'),
										store = accountManager.getStore();
									Ext.ux.Toast.msg("温馨提示", message + "成功");
									me.close();
									store.load();
								},
								exception : function(from, action, json) {
									Ext.ux.Toast.msg("温馨提示", json.responseStatus.message);
								}
							});
						}
					}]
                });
		}
		return this.form;
	},
	isUpdate: null,
	bindData: function(record,isUpdate){
		var me = this,
			form = me.getForm().getForm();
//			loginAccount = form.findField('loginAccount'),
//			passWord = form.findField('passWord')
		form.reset();
		form.loadRecord(record);
		me.isUpdate = isUpdate;
	},
    initComponent: function() {
        var me = this;
        Ext.applyIf(me, {
            items: [me.getForm()]
        });
        me.callParent(arguments);
    }
});

